home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / calcs.txt < prev    next >
Text File  |  1994-09-21  |  48KB  |  1,148 lines

  1.  
  2.               EXPONENTS
  3.   The computer understands exponents:
  4. PRINT 4^3
  5. That line makes the computer use the number 4, three times. The 
  6. computer will multiply together those three 4's, like this: 4 
  7. times 4 times 4. Since ``4 times 4 times 4'' is 64, the computer 
  8. will print:
  9. 64
  10.   The symbols +, -, *, /, and ^ are all called operations.
  11.   To solve a problem, the computer uses the three-step process 
  12. taught in algebra and the ``new math''. For example, suppose you 
  13. ask the computer to figure out 70-3^2+8/2*3. The computer will 
  14. not begin by subtracting 3 from 70; instead, it will use the 
  15. three-step process:
  16.               The problem is70-3^2+8/2*3
  17.  
  18. Step 1: get rid of ^.Now the problem is70- 9 +8/2*3
  19.  
  20. Step 2: get rid of * and /.Now the problem is70- 9 + 12
  21.  
  22. Step 3: get rid of + and -.The answer is    73
  23. In each step, it looks from left to right. For example, in step 
  24. 2, it sees / and gets rid of it before it sees *.
  25.   Although exponents are fun, the computer handles them slowly. 
  26. For example, the computer handles 3^2 more slowly than it handles 
  27. 3*3. So for speedy calculations, say 3*3 instead of 3^2.
  28.  
  29.                 Roots
  30.   What positive number, when multiplied by itself, gives 9? The 
  31. answer is 3, because 3 times itself is 9.
  32.   3 squared is 9. 3 is called the square root of 9.
  33.   To make the computer deduce the square root of 9, type this:
  34. PRINT SQR(9)
  35. The computer will print 3.
  36.   When you tell the computer to PRINT SQR(9), make sure you put 
  37. the parentheses around the 9.
  38.   The symbol SQR is called a function. The number in parentheses 
  39. (9) is called the function's input (or argument or parameter). 
  40. The answer, which is 3, is called the function's output (or 
  41. value).
  42.   SQR(9) gives the same answer as 9^.5. Most computers handle 
  43. SQR(9) more quickly than 9^.5.
  44.   Cube roots What number, when multiplied by itself and then 
  45. multiplied by itself again, gives 64? The answer is 4, because 4 
  46. times 4 times 4 is 64. The answer (4) is called the cube root of 
  47. 64.
  48.   Here's how to make the computer find the cube root of 64:
  49. PRINT 64^(1/3)
  50. The computer will print 4.
  51.                                                         EXP
  52.                                          The letter ``e'' stands 
  53. for a special number, which is approximately 2.718281828459045. 
  54. You can memorize that number easily, if you pair the digits:
  55. 2.7 18 28 18 28 45 90 45
  56.                                          That weird number is 
  57. important in calculus, radioactivity, biological growth, and 
  58. other areas of science. It's calculated by this formula:
  59.         1    1      1        1          1    
  60. e = 1 +   +     +       +         +            . . .
  61.         1   1*2   1*2*3   1*2*3*4   1*2*3*4*5
  62. Therefore:
  63.              1      1        1          1 
  64. e = 1 + 1 +     +       +         +           + . . .
  65.              2      6       24         120
  66.                                          EXP(X) means eX. For 
  67. example, EXP(3) means e3, which is e*e*e, which is:
  68. 2.718281828459045*2.718281828459045*2.718281828459045
  69. EXP(4) means e4, which is e*e*e*e. EXP(3.1) means e3.1, which is 
  70. more than e3 but less than e4.
  71.                                          Here's a practical 
  72. application. Suppose you put $732 in a savings account, and the 
  73. bank promises to give you 5% annual interest ``compounded 
  74. continuously''. How much money will you have at the end of the 
  75. year? The answer is 732*EXP(.05).
  76.              Logarithms
  77.   Here are some powers of 2:
  78. X      2X
  79. 1       2
  80. 2       4
  81. 3       8
  82. 4      16
  83. 5      32
  84. 6      64
  85.   To compute the logarithm-base-2 of a number, find the number in 
  86. the right-hand column; the answer is in the left column. For 
  87. example, the logarithm-base-2 of 32 is 5. The logarithm-base-2 of 
  88. 15 is slightly less than 4.
  89.   The logarithm-base-2 of 64 is 6. That fact is written:
  90. log2 64 is 6
  91. It's also written:
  92. log 64
  93.        is 6
  94. log 2
  95. To make the computer find the logarithm-base-2 of 64, say:
  96. PRINT LOG(64)/LOG(2)
  97. The computer will print 6.
  98.   Here are some powers of 10:
  99. X     10X
  100. 1      10
  101. 2     100
  102. 3    1000
  103. 4   10000
  104. 5  100000
  105. The logarithm-base-10 of 100000 is 5. The logarithm-base-10 of 
  106. 1001 is slightly more than 3.
  107.   The logarithm-base-10 of 10000 is 4. That fact is written:
  108. log10 10000 is 4
  109. It's also written:
  110. log 10000
  111.           is 4
  112.  log 10
  113. To make the computer do that calculation, say:
  114. PRINT LOG(10000)/LOG(10)
  115. The computer will print 4.
  116.   The logarithm-base-10 is called the common logarithm, and is 
  117. the kind of logarithm used in high school and chemistry. So if 
  118. you're studying chemistry and your textbook tells you to find the 
  119. logarithm of 10000, the textbook means the logarithm-base-10 of 
  120. 10000, which is LOG(10000)/LOG(10).
  121.   What happens if you forget the base, and say just LOG(10000) 
  122. instead of LOG(10000)/LOG(10)? If you say just LOG(10000), the 
  123. computer will find the natural logarithm of 10000, which is loge 
  124. 10000 (where e is 2.718281828459045), which isn't what your 
  125. chemistry textbook wants.
  126.  
  127.               CONTRASTS
  128.                                          The computer's notation 
  129. resembles that of arithmetic and algebra, but beware of these 
  130. contrasts. . . . 
  131.  
  132.                                                   Multiplication
  133.                                          To make the computer 
  134. multiply, you must type an asterisk:
  135. Traditional notation                                 Computer 
  136. notation
  137. 2n                                                   2*N
  138. 5(n+m)                                               5*(N+M)
  139. nm                                                   N*M
  140.  
  141.                                                      Exponents
  142.                                          Put an exponent in 
  143. parentheses, if it contains an operation:
  144. Traditional notation                                 Computer 
  145. notation
  146.  
  147. xn+2                                                 X^(N+2)
  148.  
  149. x3n                                                  X^(3*N)
  150.  
  151. 52/3                                                 5^(2/3)
  152.  
  153. 234                                                  2^(3^4)
  154.  
  155.                                                      Fractions
  156.                                          Put a fraction's 
  157. numerator in parentheses, if it contains addition or subtraction:
  158. Traditional notation                                 Computer 
  159. notation
  160.  
  161. a+b
  162.                                                      (A+B)/C
  163.   c
  164.  
  165.  
  166. k-20
  167.                                                      (K-20)/6
  168.   6
  169.                                          Put a denominator in 
  170. parentheses, if it contains addition, subtraction, 
  171. multiplication, or division:
  172. Traditional notation                                 Computer 
  173. notation
  174.  
  175.   5  
  176.                                                      5/(3+X)
  177. 3+x
  178.  
  179.  
  180. 5a3
  181.                                                      5*A^3/(4*B)
  182. 4b
  183.  
  184.                                                    Mixed numbers
  185.                                          A mixed number is a 
  186. number that contains a fraction. For example, 9½ is a mixed 
  187. number. When you write a mixed number, put a plus sign before its 
  188. fraction:
  189. Traditional notation                                 Computer 
  190. notation
  191. 9½                                                   9+1/2
  192.                                          If you're using the 
  193. mixed number in a further calculation, put the mixed number in 
  194. parentheses:
  195. Traditional notation                                 Computer 
  196. notation
  197. 7-2¼                                                 7-(2+1/4)
  198.  
  199.  
  200.               STRIPPING
  201.   Sometimes the computer prints too much information: you wish 
  202. the computer would print less, to save yourself the agony of 
  203. having to read excess information that's irrelevant to your 
  204. needs. Whenever the computer prints too much information about a 
  205. numerical answer, use ABS, INT, or SGN.
  206.   ABS removes any minus sign. For example, the ABS of -7.926 is 
  207. 7.926. So if you say PRINT ABS(-7.926), the computer will print 
  208. just 7.926.
  209.   INT removes any digits after the decimal point, and rounds the 
  210. number down to an integer that's lower. For example, the INT of 
  211. 7.926 is 7 (because 7 is an integer that's lower than 7.926);  
  212. the INT of -7.926 is -8  (because -8 is lower than -7.926).
  213.   SGN removes all the digits and replaces them by a 1 ___ unless 
  214. the number is 0. For example, the SGN of 7.926 is 1. The SGN of 
  215. -7.926 is -1. The SGN of 0 is just 0.
  216.   ABS, INT, and SGN are all called stripping functions or 
  217. strippers or diet functions or diet pills, because they strip 
  218. away the number's excess fat and reveal just the fundamentals 
  219. that interest you.
  220.   Here are more details about those three functions. . . . 
  221.  
  222.                  ABS
  223.   To find the absolute value of a negative number, just omit the 
  224. number's minus sign. For example, the absolute value of -7 is 7.
  225.   The absolute value of a positive number is the number itself. 
  226. For example, the absolute value of 7 is 7.
  227.   To make the computer find the absolute value of -7, type this:
  228. PRINT ABS(-7)
  229. The computer will print:
  230. 7
  231.   Like SQR, ABS is a function: you must put parentheses after the 
  232. ABS.
  233.   ABS helps you solve math and physics problems that involve 
  234. ``distance''. For example, this program computes the distance 
  235. between two numbers:
  236. 10 PRINT "I WILL FIND THE DISTANCE BETWEEN TWO NUMBERS."
  237. 20 INPUT "WHAT'S THE FIRST NUMBER";X
  238. 30 INPUT "WHAT'S THE SECOND NUMBER";Y
  239. 40 PRINT "THE DISTANCE BETWEEN THOSE NUMBERS IS";ABS(X-Y)
  240. For example, if X is 4, and Y is 7, then the distance between 
  241. those two numbers is ABS(4-7), which is ABS(-3), which is 3. If 
  242. you reverse those two numbers, so that X is 7 and Y is 4, the 
  243. distance between them is ABS(7-4), which is ABS(3), which is 
  244. still 3.
  245.                                                         INT
  246.                                          If you round 17.9 to an 
  247. integer, what do you get?
  248.                                          If you round 17.9 to the 
  249. nearest integer, you get 18. If you round 17.9 down to an 
  250. integer, you get 17. If you round 17.9 up to an integer, you get 
  251. 18.
  252.                                          To make the computer 
  253. round 17.9 down to an integer, type this:
  254. PRINT INT(17.9)
  255. The computer will print:
  256. 17
  257.                                          Notice that INT rounds 
  258. down. INT(17.9) tells the computer to round 17.9 down to an 
  259. integer; the computer gets 17.
  260.                                          Like SQR and ABS, INT is 
  261. a function: you must put parentheses after the INT.
  262.                                          If you give this command 
  263. ___ 
  264. PRINT INT(-5.2)
  265. what number will the computer print? Will it print -5? Or will it 
  266. print -6 instead? Answer: the computer will print -6, because if 
  267. today's temperature is -5.2 degrees, and you round the 
  268. temperature down, the temperature becomes colder: -6 degrees. INT 
  269. rounds down. INT(-5.2) is -6.
  270.                                          INT(54) is simply 54.
  271.                                          To explore further the 
  272. mysteries of rounding, run this program:
  273. 10 INPUT "WHAT'S YOUR FAVORITE NUMBER";X
  274. 20 PRINT INT(X)
  275. 30 PRINT -INT(-X)
  276. 40 PRINT INT(X+.5)
  277. In that program, line 10 asks you to type a number X. Line 20 
  278. prints your number rounded down; line 30 prints your number 
  279. rounded up; and line 40 prints your number rounded to the nearest 
  280. integer. For example, if you input 17.9, line 20 makes the 
  281. computer print 17.9 rounded down (which is 17), line 30 makes the 
  282. computer print 17.9 rounded up (which is 18), and line 40 makes 
  283. the computer print 17.9 rounded to the nearest integer (which is 
  284. 18).
  285.                                          Here's the rule: if X is 
  286. a number, INT(X) rounds X down; -INT(-X) rounds X up; INT(X+.5) 
  287. rounds X to the nearest integer.
  288.                                          Rounding down and 
  289. rounding up are useful in the supermarket. . . . 
  290.                                          Suppose some items are 
  291. marked ``30¢ each'', and you have only two dollars. How many can 
  292. you buy? Two dollars divided by 30¢ is 6.66667; rounding down to 
  293. an integer, you can buy 6.
  294.                                          Suppose some items are 
  295. marked ``3 for a dollar'', and you want to buy just one of them. 
  296. How much will the supermarket charge you? One dollar divided by 3 
  297. is 33.3333¢; rounding up to an integer, you will be charged 34¢.
  298.   By using INT, you can do fancier kinds of rounding:
  299. to round X to the nearest thousand,   ask for INT(X/1000+.5)*1000
  300. to round X to the nearest thousandth, ask for INT(X/.001+.5)*.001
  301.   This program rounds a number, so that it will have just a few 
  302. digits after the decimal point:
  303. 10 INPUT "WHAT'S YOUR FAVORITE NUMBER";X
  304. 20 INPUT "HOW MANY DIGITS WOULD YOU LIKE AFTER ITS DECIMAL 
  305. POINT";D
  306. 30 B=10^-D
  307. 40 PRINT "YOUR NUMBER ROUNDED IS";INT(X/B+.5)*B
  308. Here's a sample run:
  309. WHAT'S YOUR FAVORITE NUMBER? 4.28631
  310. HOW MANY DIGITS WOULD YOU LIKE AFTER ITS DECIMAL POINT? 2
  311. YOUR NUMBER ROUNDED IS 4.29
  312.  
  313.                        SGN
  314.   If a number is negative, its sign is -1. For example, the sign 
  315. of -546 is -1.
  316.   If a number is positive, its sign is +1. For example the sign 
  317. of 8231 is +1.
  318.   The sign of 0 is 0.
  319.   The computer's abbreviation for ``sign'' is ``SGN''. So if you 
  320. say ___ 
  321. PRINT SGN(-546)
  322. the computer will print the sign of -546; it will print -1.
  323.   If you say ___ 
  324. PRINT SGN(8231)
  325. the computer will print the sign of 8231; it will print 1.
  326.   If you say ___ 
  327. PRINT SGN(0)
  328. the computer will print the sign of 0; it will print 0.
  329.   SGN is the opposite of ABS. Let's see what both functions do to 
  330. -7.2. ABS removes the minus sign, but leaves the digits:
  331. ABS(-7.2) is 7.2
  332. SGN removes the digits, but leaves the minus sign:
  333. SGN(-7.2) is -1
  334.   The Latin word for sign is signum. Most mathematicians prefer 
  335. to talk in Latin ___ they say ``signum'' instead of ``sign'' ___ 
  336. because the English word ``sign'' sounds too much like the 
  337. trigonometry word ``sine''. So mathematicians call SGN the signum 
  338. function.
  339.  
  340.  
  341.            RANDOM NUMBERS
  342.   Usually, the computer is predictable: it does exactly what you 
  343. say. But sometimes, you want the computer to be unpredictable.
  344.   For example, if you're going to play a game of cards with the 
  345. computer and tell the computer to deal, you want the cards dealt 
  346. to be unpredictable. If the cards were predictable ___ if you 
  347. could figure out exactly which cards you and the computer would 
  348. be dealt ___ the game would be boring.
  349.   In many other games too, you want the computer to be 
  350. unpredictable, to ``surprise'' you. Without an element of 
  351. surprise, the game would be boring.
  352.   Being unpredictable increases the pleasure you derive from 
  353. games ___ and from art. To make the computer act artistic, and 
  354. create a new original masterpiece that's a ``work of art'', you 
  355. need a way to make the computer get a ``flash of inspiration''. 
  356. Flashes of inspiration aren't predictable: they're surprises.
  357.   Here's how to make the computer act unpredictably. . . . 
  358.  
  359.            Random integers
  360.   This program makes the computer print an unpredictable number 
  361. from 1 to 5:
  362. 10 RANDOMIZE
  363. 20 PRINT RND(5)
  364.   Unpredictable numbers are called random numbers. Line 10 tells 
  365. the computer to make the numbers be completely unpredictable, 
  366. completely random. Line 20 makes the computer print a random 
  367. number from 1 to 5, so the computer will print. The computer's 
  368. choice will be a surprise.
  369.   For example, when you type RUN, the computer might print 3. If 
  370. you run the program a second time, the computer might print a 
  371. different number (1 or 2 or 4 or 5), or it might print the same 
  372. number (3). You can't predict which number the computer will 
  373. print. The only thing you can be sure of is: the number will be 
  374. from 1 to 5.
  375.   For YOUR computer, you'll probably have to write the program 
  376. slightly differently. To find out your computer's peculiarities, 
  377. check the ``Versions of BASIC'' appendix. For example, if you 
  378. have an IBM PC or clone, that appendix tells you to say 
  379. ``RANDOMIZE TIMER'' instead of just ``RANDOMIZE'', and to say 
  380. 1+INT(RND*5) instead of just RND(5).
  381.   To make the computer print many such random numbers, say GO TO:
  382. 10 RANDOMIZE
  383. 20 PRINT RND(5)
  384. 30 GO TO 20
  385. The computer will print many numbers, like this:
  386.  3
  387.  2
  388.  4
  389.  4
  390.  1
  391.  3
  392.  5
  393.  2
  394.  2
  395.  5
  396.  etc.
  397. Each number will be 1 or 2 or 3 or 4 or 5. The order in which the 
  398. computer prints them is unpredictable. The program's an infinite 
  399. loop: it won't stop until you abort it. If you run the
  400. program again, the pattern will be different; for example, it 
  401. might be:
  402.  1
  403.  4
  404.  3
  405.  3
  406.  2
  407.  5
  408.  1
  409.  1
  410.  2
  411.  etc.
  412.                                          When you run that 
  413. program, the numbers will fly up the screen faster than you can 
  414. read. To make the numbers easier to read, make the computer print 
  415. them across instead of down; make the computer print like this:
  416.  3  2  4  4  1  3  5  2  2  5  etc.
  417. To do that, put a semicolon in the PRINT statement:
  418. 10 RANDOMIZE
  419. 20 PRINT RND(5);
  420. 30 GO TO 20
  421.                                          That program prints 
  422. random numbers up to 5. To see random numbers up to 1000, say 
  423. RND(1000):
  424. 10 RANDOMIZE
  425. 20 PRINT RND(1000);
  426. 30 GO TO 20
  427. The computer will print something like this:
  428.  485  729  8  537  1000  13  1  842  842  156  1000  972  etc.
  429.  
  430.                                                    Guessing game
  431.                                          This program plays a 
  432. guessing game:
  433. 10 RANDOMIZE
  434. 20 PRINT "I'M THINKING OF A NUMBER FROM 1 TO 100."
  435. 30 C=RND(100)
  436. 40 INPUT "WHAT DO YOU THINK MY NUMBER IS";G
  437. 50 IF G<C THEN PRINT "YOUR GUESS IS TOO LOW.": GO TO 40
  438. 60 IF G>C THEN PRINT "YOUR GUESS IS TOO HIGH.": GO TO 40
  439. 70 PRINT "CONGRATULATIONS!  YOU FOUND MY NUMBER!"
  440.                                          Line 20 makes the 
  441. computer say:
  442. I'M THINKING OF A NUMBER FROM 1 TO 100.
  443. Line 30 makes the computer think of a random number from 1 to 
  444. 100; the computer's number is called ``C''. Line 40 asks the 
  445. human to guess the number; the guess is called ``G''.
  446.                                          If the guess is less 
  447. than the computer's number, line 50 makes the computer say ``YOUR 
  448. GUESS IS TOO LOW'' and then GO TO 40, which lets the human guess 
  449. again. If the guess is greater than the computer's number, line 
  450. 60 makes the computer say ``YOUR GUESS IS TOO HIGH'' and then GO 
  451. TO 40.
  452.                                          When the human guesses 
  453. correctly, the computer arrives at line 70, which prints:
  454. CONGRATULATIONS!  YOU FOUND MY NUMBER!
  455.                                          Here's a sample run:
  456. RUN
  457. I'M THINKING OF A NUMBER FROM 1 TO 100.
  458. WHAT DO YOU THINK MY NUMBER IS? 54
  459. YOUR GUESS IS TOO LOW.
  460. WHAT DO YOU THINK MY NUMBER IS? 73
  461. YOUR GUESS IS TOO HIGH.
  462. WHAT DO YOU THINK MY NUMBER IS? 62
  463. YOUR GUESS IS TOO LOW.
  464. WHAT DO YOU THINK MY NUMBER IS? 68
  465. YOUR GUESS IS TOO LOW.
  466. WHAT TO YOU THINK MY NUMBER IS? 70
  467. YOUR GUESS IS TOO HIGH.
  468. WHAT DO YOU THINK MY NUMBER IS? 69
  469. CONGRATULATIONS!  YOU FOUND MY NUMBER!
  470.           Dice
  471.   This program makes the computer roll a pair of dice:
  472. 10 RANDOMIZE
  473. 20 PRINT "I'M ROLLING A PAIR OF DICE"
  474. 30 A=RND(6)
  475. 40 PRINT "ONE OF THE DICE SAYS";A
  476. 50 B=RND(6)
  477. 60 PRINT "THE OTHER SAYS";B
  478. 70 PRINT "THE TOTAL IS";A+B
  479. Line 20 makes the computer say:
  480. I'M ROLLING A PAIR OF DICE
  481. Each of the dice has 6 sides. Lines 30 and 40 roll one of the 
  482. dice, by picking a number from 1 to 6. Lines 50 and 60 roll the 
  483. other. Line 70 prints the total.
  484.   Here's a sample run:
  485. I'M ROLLING A PAIR OF DICE
  486. ONE OF THE DICE SAYS 3
  487. THE OTHER SAYS 5
  488. THE TOTAL IS 8
  489. Here's another run:
  490. I'M ROLLING A PAIR OF DICE
  491. ONE OF THE DICE SAYS 6
  492. THE OTHER SAYS 4
  493. THE TOTAL IS 10
  494.  
  495.                                              Coin flipping
  496.                              This program makes the computer flip 
  497. a coin:
  498. 10 RANDOMIZE
  499. 20 IF RND(2)=1 THEN PRINT "HEADS" ELSE PRINT "TAILS"
  500.                              RND(2) is a random number from 1 to 
  501. 2; so it's either 1 or 2. If it's 1, line 20 makes the computer 
  502. says HEADS; if it's 2 instead, line 20 makes the computer say 
  503. TAILS.
  504.                              Until you type RUN, you won't know 
  505. which way the coin will flip; the choice is random. Each time you 
  506. type RUN, the computer will flip the coin again; each time, the 
  507. outcome is unpredictable.
  508.                              (Warning: if your computer's too 
  509. stupid to understand the word ELSE, you must retype line 20; the 
  510. ``Versions of BASIC'' appendix explains how.)
  511.                              Bets Let's permit the human to bet 
  512. on whether the computer will say HEADS or TAILS. Here's how:
  513. 10 RANDOMIZE
  514. 15 INPUT "DO YOU WANT TO BET ON HEADS OR TAILS";B$                         
  515. 16 IF B$<>"HEADS" AND B$<>"TAILS" THEN PRINT "SAY HEADS OR 
  516. TAILS": GO TO 15
  517. 20 IF RND(2)=1 THEN C$="HEADS" ELSE C$="TAILS"
  518. 30 PRINT "THE COIN SAYS ";C$                          
  519. 40 IF C$=B$ THEN PRINT "YOU WIN" ELSE PRINT "YOU LOSE"
  520.                              Line 15 makes the computer ask:
  521. DO YOU WANT TO BET ON HEADS OR TAILS?
  522. Line 16 makes sure the human says HEADS or TAILS: if the human's 
  523. answer isn't HEADS and isn't TAILS, the computer gripes. Lines 20 
  524. and 30 make the computer flip a coin. Line 40 determines whether 
  525. the human won or lost the bet.
  526.                              Here's a sample run:
  527. DO YOU WANT TO BET ON HEADS OR TAILS? HEADS
  528. THE COIN SAYS TAILS
  529. YOU LOSE
  530.                              Here's another:
  531. DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
  532. THE COIN SAYS TAILS
  533. YOU WIN
  534.                              Here's another:
  535. DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
  536. THE COIN SAYS HEADS
  537. YOU LOSE
  538.  
  539.   Money To make the program more fun, let the human use money 
  540. when betting:
  541. 1 RANDOMIZE
  542. 2 S=100                                                                      
  543. 3 PRINT "YOU HAVE";S;"DOLLARS"                                               
  544. 10 INPUT "HOW MANY DOLLARS DO YOU WANT TO BET";B                             
  545. 11 IF B>S THEN PRINT "YOU DON'T HAVE THAT MUCH! YOU MUST BET 
  546. LESS!": GO TO 10
  547. 12 IF B<0 THEN PRINT "YOU CAN'T BET LESS THAN NOTHING!": GO TO 10            
  548. 13 IF B=0 THEN PRINT "I GUESS YOU DON'T WANT TO BET ANYMORE": GO 
  549. TO 100      
  550. 15 INPUT "DO YOU WANT TO BET ON HEADS OR TAILS";B$
  551. 16 IF B$<>"HEADS" AND B$<>"TAILS" THEN PRINT "SAY HEADS OR 
  552. TAILS": GO TO 15
  553. 20 IF RND(2)=1 THEN C$="HEADS" ELSE C$="TAILS"
  554. 30 PRINT "THE COIN SAYS ";C$
  555. 40 IF C$=B$ THEN PRINT "YOU WIN";B;"DOLLARS": S=S+B: GO TO 3       
  556. 50 PRINT "YOU LOSE";B;"DOLLARS": S=S-B: IF S>0 THEN GO TO 3        
  557. 60 PRINT "YOU'RE BROKE!  TOO BAD!"                                 
  558. 100 PRINT "THANKS FOR PLAYING WITH ME!  YOU WERE FUN TO PLAY 
  559. WITH!"
  560. 110 PRINT "I HOPE YOU PLAY AGAIN SOMETIME"                         
  561.   Lines 2 and 3 make the computer say:
  562. YOU HAVE 100 DOLLARS
  563. Line 10 makes the computer ask:
  564. HOW MANY DOLLARS DO YOU WANT TO BET?
  565. Lines 11-13 make sure the bet is reasonable. Lines 15 and 16 get 
  566. the human to bet on heads or tails. Lines 20 and 30 flip the 
  567. coin. Lines 40 and 50 determine whether the human won or lost the 
  568. bet, and then send the computer back to line 3 for another round 
  569. (if the human isn't broke yet). Lines 60-110 say good-bye to the 
  570. human.
  571.   Here's a sample run:
  572. YOU HAVE 100 DOLLARS
  573. HOW MANY DOLLARS DO YOU WANT TO BET? 120
  574. YOU DON'T HAVE THAT MUCH!  YOU MUST BET LESS!
  575. HOW MANY DOLLARS DO YOU WANT TO BET? 75
  576. DO YOU WANT TO BET ON HEADS OR TAILS? HEADS
  577. THE COIN SAYS TAILS
  578. YOU LOSE 75 DOLLARS
  579. YOU HAVE 25 DOLLARS
  580. HOW MANY DOLLARS DO YOU WANT TO BET? 10
  581. DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
  582. THE COIN SAYS TAILS
  583. YOU WIN 10 DOLLARS
  584. YOU HAVE 35 DOLLARS
  585. HOW MANY DOLLARS DO YOU WANT TO BET? 35
  586. DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
  587. THE COIN SAYS HEADS
  588. YOU LOSE 35 DOLLARS
  589. YOU'RE BROKE!  TOO BAD!
  590. THANKS FOR PLAYING WITH ME!  YOU WERE FUN TO PLAY WITH!
  591. I HOPE YOU PLAY AGAIN SOMETIME
  592.   Displaying all the dollars To make the output prettier, insert 
  593. these lines:
  594. 3 PRINT
  595. 4 PRINT "YOU HAVE";S;"DOLLARS!  HERE THEY ARE:"
  596. 5 FOR I = 1 TO S
  597. 6     PRINT "$";
  598. 7 NEXT
  599. 8 PRINT
  600.  
  601.  
  602.   Now the run looks like this:
  603. RUN
  604.  
  605. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  606. $$$$$$$$$$$$$$$
  607. $$$$$$$$$$$$$$$$$$$$
  608. HOW MANY DOLLARS DO YOU WANT TO BET? 120
  609. YOU DON'T HAVE THAT MUCH!  YOU MUST BET LESS!
  610. HOW MANY DOLLARS DO YOU WANT TO BET? 75
  611. DO YOU WANT TO BET ON HEADS OR TAILS? HEADS
  612. THE COIN SAYS TAILS
  613. YOU LOST 75 DOLLARS
  614.  
  615. YOU HAVE 25 DOLLARS!  HERE THEY ARE:
  616. $$$$$$$$$$$$$$$$$$$$$$$$$
  617. HOW MANY DOLLARS DO YOU WANT TO BET? 10
  618. DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
  619. THE COIN SAYS TAILS
  620. YOU WIN 10 DOLLARS
  621.  
  622. YOU HAVE 35 DOLLARS!  HERE THEY ARE:
  623. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  624. HOW MANY DOLLARS DO YOU WANT TO BET? 35
  625. DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
  626. THE COIN SAYS HEADS
  627. YOU LOSE 35 DOLLARS
  628. YOU'RE BROKE!  TOO BAD!
  629. THANKS FOR PLAYING WITH ME!  YOU WERE FUN TO PLAY WITH!
  630. I HOPE YOU PLAY AGAIN SOMETIME
  631.  
  632.                   Your friends
  633.   This program makes the computer reveal the secret desires of 
  634. your friends:
  635. 10 RANDOMIZE
  636. 20 INPUT "TYPE THE NAME OF SOMEONE YOU LIKE...";N$
  637. 30 IF RND(3)=1 THEN PRINT N$;" WANTS TO TICKLE YOUR TOES" ELSE 
  638. PRINT N$;" WANTS
  639. YOU TO WIGGLE YOUR NOSE"
  640. 40 GO TO 20
  641.   Line 20 makes the computer ask:
  642. TYPE THE NAME OF SOMEONE YOU LIKE...?
  643. Suppose you say SUE. Then N$ is SUE. Line 30 makes the computer 
  644. pick a random number up to 3. If the number is 1, the computer 
  645. will say SUE WANTS TO TICKLE YOUR TOES; but if the number is 2 or 
  646. 3 instead, the computer will say SUE WANTS YOU TO WIGGLE YOUR 
  647. NOSE. Line 40 makes the computer go back to line 20 and analyze 
  648. your other friends also.
  649.   In that program, the chance is only 1 out of 3 that the 
  650. computer will say TICKLE YOUR TOES. The chance is 2 out of 3 that 
  651. the computer will say WIGGLE YOUR NOSE instead.
  652.   Get together with your friends and run that program. For 
  653. exciting results, take off your shoes, and put lipstick on your 
  654. nose.
  655.  
  656.                  Daily horoscope
  657.   This program predicts what will happen to you today:
  658. 10 RANDOMIZE
  659. 20 PRINT "YOU WILL HAVE A ";
  660. 30 R=RND(5)
  661. 40 IF R=1 THEN PRINT "WONDERFUL";
  662. 50 IF R=2 THEN PRINT "BETTER-THAN-AVERAGE";
  663. 60 IF R=3 THEN PRINT "SO-SO";
  664. 70 IF R=4 THEN PRINT "WORSE-THAN-AVERAGE";
  665. 80 IF R=5 THEN PRINT "TERRIBLE";
  666. 90 PRINT " DAY TODAY"
  667.   The computer will say ___ 
  668. YOU WILL HAVE A WONDERFUL DAY TODAY
  669. or ___ 
  670. YOU WILL HAVE A TERRIBLE DAY TODAY
  671. or some in-between comment.
  672.   For inspiration, run that program when you get up in the 
  673. morning.
  674.                                                         Random decimals
  675.                                                      You've seen 
  676. that RND(5) is a random number from 1 to 5: it's 1 or 2 or 3 or 4 
  677. or 5. To get a random decimal between 0 and 1, say just RND 
  678. instead of RND(5).
  679.                                                      The decimal 
  680. that RND produces is at least 0 and is less than 1, so it can be 
  681. any decimal from 0.0000000 to 0.9999999. For example, the decimal 
  682. might be 0.2845918.
  683.                                                      Suppose you 
  684. want the computer to maybe print LOVE. Here's how to make the 
  685. probability of printing LOVE be 37 percent:
  686. 10 RANDOMIZE
  687. 20 IF RND<.37 THEN PRINT "LOVE"
  688.  
  689.  
  690.                  CHARACTER CODES
  691.   Each character has a code number. For example, the code number 
  692. for ``A'' is 65; the code number for ``B'' is 66; the code number 
  693. for ``C'' is 67; etc.
  694.   Those code numbers form the American Standard Code for 
  695. Information Interchange, which is abbreviated ASCII, which is 
  696. pronounced ``ass key''. Programmers say, ``the ASCII code number 
  697. for A is 65''.
  698.   If you say ___ 
  699. PRINT ASC("A")
  700. the computer will print the ASCII code number for ``A''. It will 
  701. print:
  702.  65
  703.   Similarly, if you say PRINT ASC(``B''), the computer will print 
  704. 66.
  705.   If you say ___ 
  706. PRINT CHR$(65)
  707. the computer will print the CHaRacter whose code number is 65. It 
  708. will print:
  709. A
  710.   The code number for a capital ``A'' is 65, capital ``B'' is 66, 
  711. capital ``C'' is 67, etc. The code number for a small ``a'' is 
  712. 97, small ``b'' is 98, small ``c'' is 99, etc. The code number 
  713. for the digit ``0'' is 48, the digit ``1'' is 49, the digit ``2'' 
  714. is 50, etc.
  715.   Here's the complete list of code numbers, from 33 to 126:
  716. Character: !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /  0  1... 9  
  717. :  ;  <  =  >
  718. Code #:33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49...57 58 
  719. 59 60 61 62
  720.  
  721. Character: ?  @  A  B... Z  [  \  ]  ^  _  `  a  b... z   {   |   
  722. }   ~
  723. Code #:63 64 65 66...90 91 92 93 94 95 96 97 98...122 123 124 125 
  724. 126
  725.   32 is the code number for a blank space. 127 is the code number 
  726. for DELETE.
  727.   Notice that the code number for a quotation mark is 34. Here's 
  728. how to make the computer print a quotation mark:
  729. PRINT CHR$(34)
  730.   Suppose you want the computer to print:
  731. SCHOLARS THINK "HAMLET" IS A GREAT PLAY
  732. To make the computer print the quotation marks around ``HAMLET'', 
  733. use CHR$(34), like this:
  734. PRINT "SCHOLARS THINK ";CHR$(34);"HAMLET";CHR$(34);" IS A GREAT 
  735. PLAY"
  736.  
  737.                   Control codes
  738.   On the typical computer's keyboard, you'll see a key marked 
  739. CONTROL (or CTRL). That CONTROL key is near the left SHIFT key.
  740.   While you hold down the CONTROL key, you can tap the A key; 
  741. that's called a controlled A. The code number for a controlled A 
  742. is 1. Similarly, the code number for a controlled B is 2; a 
  743. controlled C is 3; etc.
  744.   Holding down the CONTROL key subtracts 64 from the code number 
  745. of the other key. For example, the A key is normally 65, so a 
  746. controlled A is 65-64, which is 1. The B key is normally 66, so a 
  747. controlled B is 66-64, which is 2.
  748.   You've seen that a controlled A has code 1, and a controlled B 
  749. has code 2. Controlled @ has code 0. In fact, by using the 
  750. CONTROL key, you can generate all the characters that have codes 
  751. from 0 to 31.
  752.  
  753.                  Graphics codes
  754.   The code numbers from 128 up to 255 are used for graphics.
  755.                                                             Explore
  756.                                                      To explore 
  757. your computer's ability to print both text and graphics, run this 
  758. program:
  759. 10 FOR I = 33 TO 255
  760. 20     PRINT I;CHR$(I);"   ";
  761. 30 NEXT
  762. The computer will print each number and its associated character, 
  763. like this:
  764.  33 !    34 "    35 #    36 $    etc.
  765. When the computer finally reaches 128, it will start printing 
  766. graphics characters.
  767.                                                      In that 
  768. program, you can try starting I at a number lower than 33. For 
  769. example, you can try starting I at 5. But be careful: some of 
  770. those low numbers will make your computer act very strangely!
  771.                                                      On the 
  772. Apple, do not say PRINT CHR$(4). The CHR$(4) might make your 
  773. computer turn on the disk drive and start wrecking your disk!
  774.                                                      If your 
  775. computer is typical, you can make it beep by saying:
  776. PRINT CHR$(7)
  777.  
  778.                   Printer codes
  779.   If you say ___ 
  780. PRINT "LOVE"
  781. the computer will print LOVE on your screen. If you say ___ 
  782. LPRINT "LOVE"
  783. the typical computer will print LOVE on paper (instead of your 
  784. screen) by using a printer.
  785.   The most popular printers are manufactured by Epson. You can 
  786. make an Epson printer do weird things on paper, by giving a 
  787. special CHR$ command.
  788.   An Epson normally prints 10 characters per inch. You can change 
  789. that:
  790. Purpose                       Command
  791. print 17 characters per inch, so the characters are 
  792. condensedLPRINT CHR$(15);
  793. cancel the condensed printing LPRINT CHR$(18);
  794.  
  795. print 12 characters per inch, so the characters are eliteLPRINT 
  796. CHR$(27)"M";
  797. cancel the elite printing     LPRINT CHR$(27)"P";
  798.  
  799. print 5 characters per inch, so the characters are enlargedLPRINT 
  800. CHR$(14);
  801. cancel the enlarged printing  LPRINT CHR$(20);
  802.  
  803. print with proportional spacing, so ``m'' is wider than 
  804. ``i''LPRINT CHR$(27)"p1";
  805. cancel the proportional spacingLPRINT CHR$(27)"p0";
  806.   When an Epson finishes printing a line of characters, it 
  807. normally jerks the paper up a sixth of an inch (so the next line 
  808. of characters is a sixth of an inch below the first line, and the 
  809. Epson prints six lines per inch). Computerists say, ``The Epson 
  810. normally feeds a sixth of an inch.'' You can change that amount:
  811. Purpose             Command
  812. feed 1/8 of an inch LPRINT CHR$(27)"0";
  813. feed about 1/10 of an inch (7/72 inch)LPRINT CHR$(27)"1";
  814. feed n/72 of an inchLPRINT CHR$(27)"A"CHR$(n);
  815. feed n/216 of an inchLPRINT CHR$(27)"3"CHR$(n);
  816. go back to normal: feed 1/6 of an inchLPRINT CHR$(27)"2";
  817.   Here are some other fancy commands you can give:
  818. Purpose             Command
  819. beep for about a tenth of a secondLPRINT CHR$(7);
  820.  
  821. print italics       LPRINT CHR$(27)"4";
  822. cancel the italics  LPRINT CHR$(27)"5";
  823.  
  824. print subscripts (tiny char. below line)LPRINT CHR$(27)"S1";
  825. print superscripts (tiny char. above line)LPRINT CHR$(27)"S0";
  826. cancel subscripts and superscriptsLPRINT CHR$(27)"T";
  827.  
  828. horizontal fill, so that ``  '' becomes ``  ''LPRINT CHR$(27)"E";
  829. cancel the horizontal fillLPRINT CHR$(27)"F";
  830. vertical fill, so that `` '' becomes `` ''LPRINT CHR$(27)"G";
  831. cancel the vertical fillLPRINT CHR$(27)"H";
  832.  
  833. move to the top of the next pageLPRINT CHR$(12);
  834. move to the right, to the next tab stopLPRINT CHR$(9);
  835. move left, to the previous characterLPRINT CHR$(9);
  836. move left, all the way to the marginLPRINT CHR$(27)"<";
  837.  
  838. make left margin be n characters wideLPRINT CHR$(27)"l"CHR$(n);
  839. make right margin be at position nLPRINT CHR$(27)"Q"CHR$(n);
  840.  
  841. cancel all previous CHR$ commandsLPRINT CHR$(27)"Q";
  842.  
  843.  
  844.  
  845.                  STRING ANALYSIS
  846.   Let's analyze the word ``SMART''.
  847.  
  848.                      Length
  849.   Since ``SMART'' has 5 characters in it, the length of ``SMART'' 
  850. is 5. If you say ___ 
  851. PRINT LEN("SMART")
  852. the computer will print the LENgth of ``SMART''; it will print:
  853.  5
  854.  
  855.                Left, right, middle
  856.   The left two characters of ``SMART'' are ``SM''. If you say ___ 
  857. PRINT LEFT$("SMART",2)
  858. the computer will print:
  859. SM
  860.   Try this program:
  861. 10 A$="SMART"
  862. 20 PRINT LEFT$(A$,2)
  863. Line 10 says A$ is ``SMART''. Line 20 says to print the left 2 
  864. characters of A$, which are ``SM''. The computer will print:
  865. SM
  866.   If A$ is ``SMART'', here are the consequences. . . . 
  867. LEN(A$) is the LENgth of A$. It is 5.
  868. LEFT$(A$,2) is the LEFT 2 characters of A$. It is ``SM''.
  869. RIGHT$(A$,2) is the RIGHT 2 characters of A$. It is ``RT''.
  870. MID$(A$,2) is the MIDdle of A$, beginning at the 2nd character. 
  871. It is ``MART''.
  872. MID$(A$,2,3) begins at the 2nd character and includes 3 
  873. characters. It's ``MAR''.
  874.   Changing the middle You can change the middle of a string, like 
  875. this:
  876. 10 A$="BUNKERS"
  877. 20 MID$(A$,2)="OWL"
  878. 30 PRINT A$
  879. Line 10 says A$ is ``BUNKERS''. Line 20 changes the middle of A$ 
  880. to ``OWL''; the change begins at the 2nd character of A$. Line 30 
  881. prints:
  882. BOWLERS
  883.   Here's a variation:
  884. 10 A$="BUNKERS"
  885. 20 MID$(A$,2)="AD AGENCY"
  886. 30 PRINT A$
  887. Line 10 says A$ is ``BUNKERS''. Line 20 says to change the middle 
  888. of A$, beginning at the 2nd character of A$. But ``AD AGENCY'' is 
  889. too long to become part of ``BUNKERS''. The computer uses as much 
  890. of ``AD AGENCY'' as will fit in ``BUNKERS''. The computer will 
  891. print:
  892. BAD AGE
  893.   Another variation:
  894. 10 A$="BUNKERS"
  895. 20 MID$(A$,2,1)="OWL"
  896. 30 PRINT A$
  897. Line 10 says A$ is ``BUNKERS''. Line 20 says to change the middle 
  898. of A$, beginning at the 2nd character of A$. But the ``,1'' makes 
  899. the computer use just 1 letter from ``OWL''. Line 30 prints:
  900. BONKERS
  901.  
  902.  
  903.            Adding strings
  904.   You can add strings together, to form a longer string:
  905. 10 A$="FAT"+"HER"
  906. 20 PRINT A$
  907. Line 10 says A$ is ``FATHER''. Line 20 makes the computer print:
  908. FATHER
  909.  
  910.         Searching in a string
  911.   You can make the computer search in a string to find another 
  912. string. To make the computer search IN the STRing ``NEEDED'' to 
  913. find ``ED'', say:
  914. PRINT INSTR("NEEDED","ED")
  915. Since ``ED'' begins at the third character of ``NEEDED'', the 
  916. computer will print:
  917.  3
  918.   If you say ___ 
  919. PRINT INSTR("NEEDED","EY")
  920. the computer will search in the string ``NEEDED'' for ``EY''. 
  921. Since ``EY'' is not in ``NEEDED'', the computer will print:
  922.  0
  923.   If you say ___ 
  924. PRINT INSTR(4,"NEEDED","ED")
  925. the computer will hunt in the string ``NEEDED'' for ``ED''; but 
  926. the hunt will begin at the 4th character of ``NEEDED''. The 
  927. computer finds the ``ED'' that begins at the 5th character of 
  928. ``NEEDED''. The computer will print:
  929.  5
  930.  
  931.                 Clock
  932.   The typical computer has a built-in clock. To set the clock to 
  933. 7 seconds after 1:45PM, type this:
  934. TIME$="13:45:07"
  935. To set the date to January 24, 1996, type this:
  936. DATE$="01-24-1996"
  937.   Afterwards, whenever you want to find out the current time and 
  938. date, type this:
  939. PRINT TIME$
  940. PRINT DATE$
  941.   If you say ___ 
  942. PRINT TIMER
  943. the computer will tell you how many seconds have elapsed since 
  944. midnight.
  945.   When you turn off the typical computer, it forgets the time and 
  946. date. When you turn it on again, tell it the new time and date.
  947.                                              String-number conversion
  948.                                          This program converts a 
  949. string to a number:
  950. 10 A$="72.6"
  951. 20 B=VAL(A$)
  952. 30 PRINT B+1
  953.                                          Line 10 says A$ is the 
  954. string ``72.6''. Line 20 says B is the numeric VALue of A$, so B 
  955. is the number 72.6. Line 30 prints:
  956.  73.6
  957.                                          VAL converts a string to 
  958. a number. The opposite of VAL is STR$, which converts a number to 
  959. a string. For example, STR$(-7.2) is the string ``-7.2''.
  960.                                          STR$(81.4) is the string 
  961. `` 81.4''. Notice that in the string `` 81.4'', the 8 comes after 
  962. a space (instead of coming after a minus sign).
  963.  
  964.                                                Repeating characters
  965.                                          Suppose you love the 
  966. letter B (because it stands for Big, Bold, and Beautiful) and 
  967. want to print ``BBBBBBBBBBBBBBBBBBBB''. Here's a short-cut:
  968. PRINT STRING$(20,"B")
  969. That tells the computer to print a string of 20 B's.
  970.                                          Here's a different way 
  971. to accomplish the same goal:
  972. PRINT STRING$(20,66)
  973. That tells the computer to print, 20 times, the character whose 
  974. ASCII code number is 66.
  975.                                          STRING$ can make the 
  976. computer repeat a single character, but not a whole word. So if 
  977. you say STRING$(20,``BLOW''), the computer will not repeat the 
  978. word ``BLOW''; instead, the computer will repeat just the first 
  979. character of ``BLOW'' (which is ``B'').
  980.  
  981.             TRIGONOMETRY
  982.   The study of triangles is called trigonometry ___ and the 
  983. computer can do it for you!
  984.   For example, look at this triangle:
  985. In that triangle, the left angle is 30°, the lower-right angle is 
  986. 90°, and the longest side (the hypotenuse) is 1 inch long.
  987.   The side opposite the 30° angle is called the sine of 30°; the 
  988. remaining side is called the cosine of 30°:
  989.   How long is the sine of 30°? How long is the cosine of 30°?
  990.   Since the longest side (the hypotenuse) is 1 inch long, and 
  991. since the sine and the cosine are shorter sides, the sine and the 
  992. cosine must each be shorter than 1 inch. So the lengths of the 
  993. sine and cosine are each less than 1. But which decimals are 
  994. they?
  995.   To find out, you can use a ruler. You'll discover that the sine 
  996. is half an inch long, and the cosine is nearly seven-eighths of 
  997. an inch long. But a faster and more accurate way to measure the 
  998. sine and cosine is to let the computer do it! Yes, the computer 
  999. can calculate triangles in its mind!
  1000.   This program makes the computer measure the sine and cosine of 
  1001. 30°:
  1002. 10 D=ATN(1)/45
  1003. 20 PRINT SIN(30*D)
  1004. 30 PRINT COS(30*D)
  1005. Line 10 is a special formula that defines D to mean ``degrees''. 
  1006. Line 20 prints the sine of 30 degrees; the computer will print:
  1007. .5
  1008. Line 30 prints the cosine of 30 degrees; the computer will print 
  1009. a decimal that's slightly less than .87.
  1010.   The computer can measure the sine and cosine of any size angle. 
  1011. Try it! For example, to make the computer print the sine and 
  1012. cosine of a 33° angle, say:
  1013. 10 D=ATN(1)/45
  1014. 20 PRINT SIN(33*D)
  1015. 30 PRINT COS(33*D)
  1016.   If you choose an angle of -33° instead of 33°, the triangle 
  1017. will dip down instead of rising up, and so the sine will be a 
  1018. negative number instead of positive.
  1019.   In lines 20 and 30, the ``*D'' is important: it tells the 
  1020. computer that you want the sine of 33 degrees. If you 
  1021. accidentally omit the ``*D'', the computer will print the sine of 
  1022. 33 radians instead. (A radian is larger than a degree. A radian 
  1023. is about 57.3 degrees. More precisely, a radian is 180/π 
  1024. degrees.)
  1025.                                                       Tangent
  1026.                                          The sine divided by the 
  1027. cosine is called the tangent. For example, to find the tangent of 
  1028. 33°, divide the sine of 33° by the cosine of 33°.
  1029.                                          To make the computer 
  1030. print the tangent of 33°, you could tell the computer to PRINT 
  1031. SIN(33*D)/COS(33*D). But to find the tangent more quickly and 
  1032. easily, just say PRINT TAN(33*D), by adding this line to your 
  1033. program:
  1034. 40 PRINT TAN(33*D)
  1035.  
  1036.                                                    Arc functions
  1037.                                          The opposite of the 
  1038. tangent is called the arctangent:
  1039. the tangent    of 30° is about .58
  1040. the arctangent of .58 is about 30°
  1041. Similarly, the opposite of the sine is called the arcsine, and 
  1042. the opposite of the cosine is called the arccosine.
  1043.                                          This program prints the 
  1044. arctangent of .58, the arcsine of .5, and the arccosine of .87:
  1045. 10 D=ATN(1)/45
  1046. 20 PRINT ATN(.58)/D
  1047. 30 X=.5: PRINT ATN(X/SQR(1-X*X))/D
  1048. 40 X=.87: PRINT 90-ATN(X/SQR(1-X*X))/D
  1049.                                          Line 10 is the special 
  1050. formula that defines ``D'' to be ``degrees''. Line 20 prints the 
  1051. arctangent of .58, in degrees. (If you omit the ``/D'', the 
  1052. computer will print the answer in radians instead of degrees.) 
  1053. Line 30 sets X equal to .5 and then prints its arcsine (by using 
  1054. a formula that combines ATN with SQR). Line 40 sets X equal to 
  1055. .87 and then prints its arccosine (by using a formula that 
  1056. combines 90 with ATN and SQR). The answer to each of the three 
  1057. problems is about 30 degrees.
  1058.  
  1059.           TYPES OF NUMBERS
  1060.   BASIC can handle three types of numbers: integers, real 
  1061. numbers, and double-precision numbers.
  1062.   Generally speaking, an integer is a number that has no decimal 
  1063. point; a real number has a decimal point but no more that 7 
  1064. digits; a double-precision number has a decimal point and more 
  1065. than 7 digits.  For example,  -27 is an integer, -27.51431 is a 
  1066. real number, and -27.514318 is a double-precision number.
  1067.   Even though -27 is an integer, -27.0 is not an integer: it's a 
  1068. real number instead. -27.000000 is a double-precision number.
  1069.   The highest permissible integer is 32767; the lowest 
  1070. permissible integer is -32768. If you try to type an integer 
  1071. higher than 32767 or lower than -32768, the computer will 
  1072. automatically add a decimal point at the end of the number, so 
  1073. that the number becomes real or double-precision. (The decimal 
  1074. point will appear in the computer's RAM but not necessarily on 
  1075. your screen.)
  1076.   Any number that contains an E (such as 7E2) is real, even if 
  1077. the number contains no decimal point or contains more than 7 
  1078. digits. To make such a number to be double-precision instead, 
  1079. type a D instead of an E (like this: 7D2).
  1080.  
  1081.               Accuracy
  1082.   The computer handles integers accurately. If you type a real 
  1083. number, the computer tries to handle it accurately, but sometimes 
  1084. makes slight mistakes with the 7th digit. If you type a 
  1085. double-precision number, the computer handles the first 16 digits 
  1086. accurately, but makes slight mistakes with the 17th digit.
  1087.  
  1088.                 Speed
  1089.   The computer handles integers quickly, real numbers slowly, and 
  1090. double-precision numbers very slowly.
  1091.  
  1092.            RAM consumption
  1093.   When the computer handles numbers, it automatically compresses 
  1094. them so that the numbers consume very little RAM. Each integer 
  1095. consumes just 2 bytes of RAM, each real number consumes 4 bytes, 
  1096. and each double-precision number consumes 8 bytes.
  1097.   For example, if your program says DIM X(50,20), the array X 
  1098. contains 50 rows of 20 numbers, making 1000 numbers altogether; 
  1099. and if each number is real (4 bytes), the entire array consumes 
  1100. 4000 bytes ___ theoretically. In practice, the array also 
  1101. contains a few extra bytes, for bureaucratic overhead. So the 
  1102. array contains slightly more than 4000 bytes ___ which is roughly 
  1103. 4K.
  1104.   On most computers, BASIC is limited to 64K: if you buy extra 
  1105. RAM beyond 64K, BASIC ignores it. Most of that 64K is consumed by 
  1106. the lines of your program, the DOS, and BASIC itself, so only a 
  1107. few K are left for arrays.
  1108.                                                      Variables
  1109.                                          An ordinary variable 
  1110. (such as X) stands for a real number. For example, you can say 
  1111. X=3.7, which makes X be the real number 3.7. If you say X=3, the 
  1112. computer will make X be 3.0 instead to make X be a real number.
  1113.                                          You can create four 
  1114. kinds of variables. A simple variable (such as X) or a variable 
  1115. that ends in an exclamation point (such as X!) is a real; a 
  1116. variable that ends in a dollar sign (such as X$) is a string; a 
  1117. variable that ends in a percent sign (such as X%) is an integer; 
  1118. a variable that ends in a number sign (such as X#) is 
  1119. double-precision.
  1120.                                          If you begin your 
  1121. program by saying ___ 
  1122. 1 DEFINT A-Z
  1123. all the simple variables (such as X) will be integers instead of 
  1124. reals.
  1125.  
  1126.                                                     What to do
  1127.                                          Write your program 
  1128. simply, without worrying about which numbers and variables are 
  1129. integers, real numbers, and double-precision numbers. But after 
  1130. your program is finished and debugged, edit the program, by 
  1131. making the following changes, which improve the program's speed 
  1132. and accuracy.
  1133.                                          All integers If your 
  1134. program doesn't involve any decimals or huge numbers, make line 1 
  1135. say DEFINT A-Z. That will turn every variable into an integer, so 
  1136. that the program runs faster and consumes less RAM.
  1137.                                          Mostly integers If your 
  1138. program involves just a few decimals or huge numbers, make line 1 
  1139. say DEFINT A-Z, and put an exclamation point after every variable 
  1140. that stands for a decimal or huge number.
  1141.                                          Extra accuracy If you 
  1142. want to perform one of the computations extra-accurately, put a 
  1143. number sign after all variables the computation involves (for 
  1144. example, say X# instead of X), and put at least 8 digits in each 
  1145. number (for example, say 2.4000000 instead of 2.4).
  1146.                                          Before you make those 
  1147. changes, check the ``Versions of BASIC'' appendix to find out 
  1148. whether your computer is different.